<?php
namespace Home\Controller;

use Common\Controller\NBaseController;
use Home\Model\ArticleModel;
use Think\Controller;

class IndexController extends NBaseController {
    //列表页,每页最多10篇
    private $pageSize = 10;

    /**
     * 首页入口
     */
    public function index(){
        $this->setSider(3);
        layout("Public/Layout/default");
        $model = new ArticleModel();
        $index = $model->where(['is_index' => 1])->find();
        $this->assign('index', $index);
        $this->display("index");
    }

    /**
     * phper分类列表页入口
     */
    public function phper(){
        $page = empty($page) ? 1 : $this->params['p'];
        $this->typePageList(1, $page);
    }

    /**
     * think分类列表也入口
     */
    public function think(){
        $page = empty($page) ? 1 : $this->params['p'];
        $this->typePageList(2, $page);
    }

    /**
     * goodlife分类列表页入口
     */
    public function goodlife(){
        $page = empty($page) ? 1 : $this->params['p'];
        $this->typePageList(3, $page);
    }

    /**
     * 普通文章详情页入口
     */
    public function detail()
    {
        $this->setSider(10);
        $id = empty($this->params['id']) ? 0 : (int)$this->params['id'];
        $model = new ArticleModel();
        $article = $model->getArticleById($id);
        $this->assign('data',$article);
        layout("Public/Layout/default");
        $this->display("detail");
    }

    /**
     * 关于页面入口
     */
    public function about()
    {
        $id = 6;//empty($this->params['id']) ? 0 : (int)$this->params['id'];
        $model = new ArticleModel();
        $article = $model->getArticleById($id);
        $this->assign('data',$article);
        layout("Public/Layout/default");
        $this->display("about");
    }

    /**
     * 设置最近更新
     * @param int $limit
     */
    private function setSider($limit = 3)
    {
        $model = new ArticleModel();
        $list = $model->getSiderList($limit);
        $this->assign('siderList', $list);
    }

    /**
     * 分类列表页数据处理
     * @param $type
     * @param $page
     */
    private function typePageList($type, $page)
    {
        $offset = ($page - 1) * $this->pageSize;
        $this->setSider(7);
        $model = new ArticleModel();
        $list = $model->where(['type' => $type, 'status' => 1])
            ->order(['publish_time' => 'DESC'])
            ->limit($offset, $this->pageSize)->select();
        $this->assign('type', $type);
        $this->assign('typeList', $list);
        layout("Public/Layout/default");
        $this->display("listp");
    }

}